package com.controller;

import com.alibaba.fastjson.JSONArray;
import com.bean.dto.Dto;
import com.bean.entity.Order;
import com.bean.entity.OrderDetail;
import com.bean.vo.OrderVo;
import com.bean.vo.query.OrderQueryVo;
import com.service.OrderService;
import com.util.DtoUtil;
import com.util.PageUtil;
import com.util.SystemUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 订单业务控制
 */
@RestController
@RequestMapping("o")
public class OrderController {

    @Autowired
    OrderService orderService;

    /** 条件分页查询客户订单信息 */
    @RequestMapping("query/customer")
    public Dto queryCustomerOrder(OrderQueryVo orderQueryVo, PageUtil<OrderVo> pa){
        SystemUtil.functionTitle("客户订单信息");

        /* 打印参数 */
        orderQueryVo.setOrderType(1);
        System.out.println(orderQueryVo);

        /* 查询数据 */
        try {
            pa = orderService.queryOrder(orderQueryVo,pa);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println(e.getMessage());
            return DtoUtil.returnFail(e.getMessage());
        }

        return DtoUtil.returnSuccess(pa);
    }

    /** 条件分页查询采购订单信息 */
    @RequestMapping("query/procurement")
    public Dto queryProcurementOrder(OrderQueryVo orderQueryVo, PageUtil<OrderVo> pa){
        SystemUtil.functionTitle("采购订单信息");

        /* 打印参数 */
        orderQueryVo.setOrderType(2);
        System.out.println(orderQueryVo);

        /* 查询数据 */
        try {
            pa = orderService.queryOrder(orderQueryVo,pa);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println(e.getMessage());
            return DtoUtil.returnFail(e.getMessage());
        }

        return DtoUtil.returnSuccess(pa);
    }


    /** 修改订单信息 */
    @RequestMapping("modify")
    public Dto modifyOrder(String obj, String order){
        SystemUtil.functionTitle("修改订单信息");
        /* 打印参数 */
        System.out.println(order);
//        System.out.println(orderDetail);
        System.out.println("obj：" + obj);
//        System.out.println(list);

        /* 数据转换：string转成json数组 */
        //json数组转成集合
        List<OrderDetail> orderDetails = JSONArray.parseArray(obj, OrderDetail.class);
        List<Order> orderList = JSONArray.parseArray("[" + order + "]", Order.class);

        System.out.println(orderDetails);
        System.out.println(orderList);

        /* 修改信息 */
        try {
            orderService.modifyOrder(orderList.get(0),orderDetails);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println(e.getMessage());
            return DtoUtil.returnFail(e.getMessage());
        }

        return DtoUtil.returnSuccess("修改成功！");
    }

    /** 添加订单信息 */
    @RequestMapping("add")
    public Dto addOrder(String obj, String order){
        SystemUtil.functionTitle("添加订单信息");
        /* 打印参数 */
        System.out.println(order);
        System.out.println("obj：" + obj);

        /* 数据转换：string转成json数组 */
        //json数组转成集合
        List<OrderDetail> orderDetails = JSONArray.parseArray(obj, OrderDetail.class);
        List<Order> orderList = JSONArray.parseArray("[" + order + "]", Order.class);

        System.out.println(orderDetails);
        System.out.println(orderList);
        /* 修改信息 */
        try {
            orderService.addOrder(orderList.get(0),orderDetails);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println(e.getMessage());
            return DtoUtil.returnFail(e.getMessage());
        }

        return DtoUtil.returnSuccess("添加成功！");
    }


}
